﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.IO;
using System.Collections.Specialized;
using Guru.Web.UI;

namespace Guru.Web.Controls.Asp
{
    public class LinkButton : System.Web.UI.WebControls.LinkButton, IMultiLanguageLoader
    {
        #region Contants       
        protected const string KEY_HTML_TEMP_NORMAL = "Guru.Web.Controls.Asp.Button";       
        public enum ButtonSizeType
        { 
            AutoSmall,          
            FixedSmallShort,
            FixedSmallLong
        }
        #endregion

        public LinkButton()
        {          
            SizeType = ButtonSizeType.FixedSmallShort;

        }
        public bool IsStandard
        {
            get;
            set;
        }
        public string CssClassIcon
        {
            set;
            get;
        }
        public ButtonSizeType SizeType
        {
            set;
            get;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.LoadTextFromResource(((GuruPage)this.Page).CurrentLanguage);
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (!this.IsStandard && string.IsNullOrEmpty(this.CssClass)&& this.Visible)
            {
                this.CssClass = "button";
                /*Size of the button*/
                switch (this.SizeType)
                {
                    case ButtonSizeType.AutoSmall:
                        this.CssClass += " buttonSmall";
                        break; 
                    case ButtonSizeType.FixedSmallShort:
                        this.CssClass += " buttonSmall buttonFixedShort";
                        break;     
                    case ButtonSizeType.FixedSmallLong:
                        this.CssClass += " buttonSmall buttonFixedLong";
                        break;                                          
                }
                
            }
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (this.IsStandard)
            {
                base.RenderContents(writer);
            }
            else
            {
                string iconClass = string.IsNullOrEmpty(this.CssClassIcon) ? string.Empty : " " + this.CssClassIcon;
                StringBuilder baseHTML = new StringBuilder();
                using (StringWriter twr = new StringWriter(baseHTML))
                {
                    using (HtmlTextWriter newWriter = new HtmlTextWriter(twr))
                    {
                        base.RenderContents(newWriter);
                        newWriter.Flush();
                        writer.Write(string.Format(HTMLTemplate.GetByKey(KEY_HTML_TEMP_NORMAL), baseHTML, iconClass));
                    }
                }
            }
        }

        public void SetText(NameValueCollection text)
        {
            this.Text = text[this.ID];
        }
    }
}
